/**
 * Copyright 2014 lerry
 * 
 * All right reserved.
 * 
 * Dec 8, 2014
 */
package com.bac.bacplatform;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

/**
 * @author lerry
 *
 */
public class FragmentHall extends BaseFragment implements OnClickListener {

	private PopupWindow mPopupWindow;
	private WebView mWebView;
	private ProgressBar mPb;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.make_card, null);
		LinearLayout lay_more = (LinearLayout) view.findViewById(R.id.lay_more);
		lay_more.setOnClickListener(this);
		
		mWebView = (WebView)view.findViewById(R.id.wb_yeepay);
		mWebView.setWebViewClient(new BacWebClient());
		mWebView.setWebChromeClient(new BacWebChromeClient());
		
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		
		mPb = (ProgressBar) view.findViewById(R.id.pb);
		mPb.setMax(100);
		
		mWebView.loadUrl("http://wechat.bac365.com/card/upload ");
		
		return view;
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_more:
			if (mPopupWindow != null && mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
 				return;
			} else {
				initmPopupWindowView();
				mPopupWindow.showAsDropDown(v, -70, 0);
				
			}
			break;
		default:
			break;
		}
	}

	private void initmPopupWindowView() {
		View customView = getLayoutInflater(getArguments()).inflate(R.layout.more, null);
		mPopupWindow = new PopupWindow(customView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		mPopupWindow.setFocusable(true);
		customView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (mPopupWindow != null && mPopupWindow.isShowing()) {
					mPopupWindow.dismiss();
					mPopupWindow = null;
				}
				return false;
			}
		});
		//拨打客服电话
		RelativeLayout call_us = (RelativeLayout) customView.findViewById(R.id.call_us);
		call_us.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//拨打电话
				String number = "4001106262";
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:"+number));
				startActivity(intent);
				mPopupWindow.dismiss();
			}
		});
		//查看关于我们
		RelativeLayout about_us = (RelativeLayout) customView.findViewById(R.id.about_us);
		about_us.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 查看关于我们协议
				Intent intentToAgree = new Intent(getActivity(),
						BacAgreementWebActivity.class);
				intentToAgree.putExtra("urltype", "about_us");
				startActivityIn(intentToAgree);
				mPopupWindow.dismiss();
			}
		});
	}
	
	private class BacWebChromeClient extends WebChromeClient {

		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			mPb.setProgress(newProgress);
			if (newProgress == 100) {
				mPb.setVisibility(View.GONE);
			}
			super.onProgressChanged(view, newProgress);
		}
	}
 
	
	private class BacWebClient extends WebViewClient{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler,
				SslError error) {
			handler.proceed();
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			// 加载本地页面，进行出错处理
			view.loadUrl("file:///android_asset/NetworkError.html");
		}
		
	}
	@Override
	public void onStop() {
		super.onStop();
		mWebView.stopLoading();
	}
	
}
